package com.coder.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.coder.model.WaveThreshold;

public class WaveDao {
	
	private SessionFactory sessionFactory;
	
	public Session getSession() {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (Exception e) {
			session = sessionFactory.openSession();
		}
    	return session;
	}

	public WaveThreshold saveOrUpdate(WaveThreshold wave){
		Session session = getSession();
		try{
			session.saveOrUpdate(wave);
		}catch(Exception e){
			e.printStackTrace();
		}
		return wave;
	}
	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	public WaveThreshold save(WaveThreshold waveThreshold) {
		Session session = getSession();
		try{
			session.saveOrUpdate(waveThreshold);
		}catch(Exception e){
			e.printStackTrace();
		}
		return waveThreshold;		
	}


	public WaveThreshold getThresholdByWave(int waveVal) {
		Session session = getSession();
		Query query = null;
		try{
			query = session.createQuery("from WaveThreshold where waveVal=?")
					.setInteger(0, waveVal);
		}catch(Exception e){
			e.printStackTrace();
		}
		return (WaveThreshold)query.list().get(0);
	}

	public void updateWaveThreshold(WaveThreshold wt) {
		
		Session session = getSession();
		try{
			session.update(wt);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**获取所有的wave与其对应的*/
	@SuppressWarnings("unchecked")
	public List<WaveThreshold> getAllWaveThreshold(){
		Session session = getSession();
		Query query = null;
		try{
			query = session.createQuery("from WaveThreshold");
		}catch(Exception e){
			e.printStackTrace();
		}
		return query.list();
	}
 
}
